package learn.config;

import learn.converter.GuiguMessageConverter;
import learn.interceptor.LoginInterceptor;
import learn.interceptor.RedisUrlCountInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.config.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EnableWebMvc // 全面接管springmvc 不建议
@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    RedisUrlCountInterceptor redisUrlCountInterceptor;

    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter();
        filter.setMethodParam("custom_method");
        return filter;
    }

    /*    *//**
     * 方法一，实现WebMvcConfigurer 加入configurer对象
     * @param configurer
     *//*
    @Override
    @Bean
    public void configurePathMatch(PathMatchConfigurer configurer){
        UrlPathHelper helper = new UrlPathHelper();
        // 不移除，后面的内容。矩阵变量功能就可以生效
        helper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(helper);
    }*/

    /**
     * 方法二，不实现WebMvcConfigurer  加入bean WebMvcConfigurer 重写 configurePathMatch方法
     */


    /**
     * 自定义内容协商转换器
     *
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new GuiguMessageConverter());
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        Map<String, MediaType> mediaTypes = new HashMap<>();
        mediaTypes.put("json", MediaType.APPLICATION_JSON);
        mediaTypes.put("xml", MediaType.APPLICATION_XML);
        mediaTypes.put("gg", MediaType.parseMediaType("application/x-guigu"));

        // 添加适配头策略
        HeaderContentNegotiationStrategy headerContentNegotiationStrategy = new HeaderContentNegotiationStrategy();
        // 指定支持解析那些参数对应的那些媒体类型
        ParameterContentNegotiationStrategy parameterContentNegotiationStrategy = new ParameterContentNegotiationStrategy(mediaTypes);
        // 浏览器参数 - 可以自定义
        parameterContentNegotiationStrategy.setParameterName("format");
        configurer.strategies(Arrays.asList(parameterContentNegotiationStrategy, headerContentNegotiationStrategy));
    }


    /**
     * 放行特殊路径 和 静态资源文件
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/", "/login", "/js/**");
        registry.addInterceptor(redisUrlCountInterceptor).addPathPatterns("/**").excludePathPatterns("/js/**");
    }


/*    @Bean
    public WebMvcRegistrations webMvcRegistrations(){
        return new WebMvcRegistrations() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return null;
            }

            @Override
            public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() {
                return null;
            }
        };
    }*/


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 访问aa路径下的所有请求都去/static下 进行匹配
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}
